package hk.linkcredit.walletservice.wallet.service.strategy;

import hk.linkcredit.walletservice.constant.Status;
import hk.linkcredit.walletservice.constant.WalletConstants;
import hk.linkcredit.walletservice.exception.BizException;
import hk.linkcredit.walletservice.wallet.domain.vo.WalletAccountInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @Author: Jamnsin
 * @Date: 2022/6/12 16:26
 */
@Slf4j
@Component(WalletConstants.WALLET_TYPE_BONUS)
public class WalletBonusStrategy implements BalanceStrategy {

    @Override
    public void checkAmount(BigDecimal applyAmt, WalletAccountInfo walletAccount) {
        log.info("校验金额 [{}] >>> applyAmt = {}, walletAccount = {}", WalletConstants.WALLET_TYPE_BONUS, applyAmt, walletAccount);

        // 获取客户钱包余额
        if (WalletConstants.WALLET_STATUS_LOCK.equals(walletAccount.getWalletStatus()) || WalletConstants.WALLET_STATUS_BONLUS_LOCK.equals(walletAccount.getWalletStatus())) {
            throw new BizException(Status.BONUS_LOCK_CANNOT_WITHDRAW);
        }

        BigDecimal walletBonus = walletAccount.getBonus().compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : walletAccount.getBonus();
        BigDecimal frozenBonus = walletAccount.getFrozenBonus().compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : walletAccount.getFrozenBonus();
        BigDecimal minWithdrawBonus = walletAccount.getMinWithdrawBonus();
        BigDecimal serviceFee = walletBonus.multiply(walletAccount.getServiceRate());
        log.debug("奖励金额及配置 >>> wallBonus = {}，frozenBonus = {}，serviceFee = {}，minWithdrawBonus = {}", walletBonus, frozenBonus, serviceFee, minWithdrawBonus);

        if (applyAmt.compareTo(minWithdrawBonus) < 0) {
            throw new BizException(Status.WALLE_WITHDRAW_CANNOT_LOWER_MIN_BONUS);
        }

        if (walletBonus.subtract(frozenBonus).compareTo(applyAmt) < 0) {
            throw new BizException(Status.WALLE_WITHDRAW_INSUFFICIENT_BONUS);
        }

    }
}
